// pages/mylikes/mylikes.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    likesTeam:[]
  },
  getLikes:function(){
    wx.request({
      url: 'http://' + app.globalData.ip +'/like/getUserLikesTeamShow',
      data: { userID: app.globalData.userInfo.openId},
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: 'POST',
      success:(res)=>{
        var teams = res.data.likes
        for (var i = 0; i < teams.length; ++i) {
          var d = new Date(teams[i]['createTime'])
          teams[i]['createTime'] = d.getFullYear() + '.' + d.getMonth() + '.' + d.getDate()
        }
        this.setData({
          likesTeam:teams
        })
      },
      fail:()=>{
        wx.showModal({
          content: '服务端错误',
        })
      }
    })
  },
  lookDetail: function (e) {
    var index = e.currentTarget.dataset.teamindex
    app.globalData.team = this.data.likesTeam[index]
    wx.navigateTo({
      url: '../teamDetail/teamDetail',
    })
  },

  /**点击取消收藏功能 */
  like: function (e) {

    var index = e.currentTarget.dataset.idx
    var teamID = e.currentTarget.dataset.teamid
    var that = this
    if (this.data.likesTeam[index].like == true) {
      //原先为true表示收藏，则点击时表示取消收藏     
      /**向后端发送请求 取消当前的收藏 */
      wx.request({
        url: 'http://' + app.globalData.ip +'/like/cancleLike',
        data: { userid: app.globalData.userInfo.openId, teamid: teamID },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        method: 'POST',
        success: function (res) {
          if (res.data.msg == 1) {
            wx.showToast({
              title: '取消收藏',
            })
            that.getLikes()
          } else {
            wx.showModal({
              content: '服务端取消收藏失败',
            })
          }
        },
        fail: function () {
          wx.showModal({
            content: '取消收藏失败',
          })
        }
      })
    } 

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getLikes()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getLikes()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})